dir=$(cd $(dirname $0) && pwd)
bin=./bin/wll
test_cmd=test.sh
test_script=test.xyz
test_input=test.in
test_output=test.out
test_expect=test.expect

cd $dir
test_dir=${1:-"../test"}
debug_flag=${2:-"ON"}

if [ -d logs ]; then rm -rf logs; fi

function test()
{
	local test_case_dir=$1
	if [[ -d $test_case_dir && -e $test_case_dir/$test_script && -e $test_case_dir/$test_input && -e $test_case_dir/$test_expect ]]
	then
		if [[ "x$debug_flag" != "xON" || ! -e $test_case_dir/PASS ]]
		then
			if [ -d $test_case_dir/logs ]; then rm -rf $test_case_dir/logs; fi
			if [ -e $test_case_dir/$test_cmd ]
			then
				time $test_case_dir/$test_cmd
			else
				time $bin $test_case_dir/$test_script $test_case_dir/$test_input 2>$test_case_dir/error.log >$test_case_dir/$test_output
			fi
			if [ -d logs ]; then mv logs $test_case_dir/; fi
			line=$(diff $test_case_dir/$test_output $test_case_dir/$test_expect | wc -l)
			if [ $line == 0 ]
			then
				if [ -e $test_case_dir/FAIL ]; then unlink $test_case_dir/FAIL; fi
				touch $test_case_dir/PASS
			        echo "$test_case_dir PASS !!!"
			else
				if [ -e $test_case_dir/PASS ]; then unlink $test_case_dir/PASS; fi
				touch $test_case_dir/FAIL
			        echo "$test_case_dir FAIL !!!"
			fi
	        fi
	fi
}

if [ -d $test_dir ]
then
	for path in $(find $test_dir | grep -v logs)
	do
		test $path
	done
fi
